package org.dei.perla.sys.device.fpc;


import java.util.ArrayList;

import org.dei.perla.parser.clauses.OnUnsupportedSampleRateOptions;
import org.dei.perla.sys.query.llqruntime.datastructures.Record;
import org.dei.perla.utils.pipe.Pipe;
import org.dei.perla.utils.waiter.Waitable;
import org.dei.perla.utils.waiter.Waiter;
import org.dei.test.llqruntime.oldtests.FakeDevice;

/**
 * @deprecated Passare a FunctionalityProxyComponent.java
 * @author diego
 */
public class FakeFPC extends FPC{
	
	private FakeDevice pFakeDevice;
	private Thread tWaitingThread;
	private Waiter<Pipe<Record>> tFakeDeviceWaiter;
	private Pipe<Record> pInPipe;
	
	//TODO Inserire un array list di pipe, per pertmettere l'esecuzione, da parte dello stesso fpc, di più query
	private Pipe<Record> pDataCollectorPipeOut;  
	
	public FakeFPC(String parText, FakeDevice parFakeDevice, Pipe<Record> parInPipe) {
		
		//super(parText, parFrame, parPipeIn, parPipeOut);
		this.pFakeDevice = parFakeDevice;
		pDataCollectorPipeOut = new Pipe<Record>("FakeFPC DataCollectorPipeOut");
		pDataCollectorPipeOut.start();
		
		pInPipe = parInPipe;
		tFakeDeviceWaiter = new Waiter<Pipe<Record>>();
		tFakeDeviceWaiter.start();
		tFakeDeviceWaiter.addWaitable(pInPipe.getWaitable());
		
		tWaitingThread = new Thread() {
			public void run() {
				
				System.out.println("FPC attende dati dalla pipe");
				Waitable<Pipe<Record>> tWaitable; 
				Record record;
				
				do {
					tWaitable = tFakeDeviceWaiter.waitNext();
					record = tWaitable.getParentComponent().dequeue();
					if (record == null) {
						System.out.println("Pipe chiusa, FakeFPC termina!");
						pInPipe.stop();
						tFakeDeviceWaiter.stop();
						pDataCollectorPipeOut.enqueue(null);
						break;
					}
					System.out.println("Record arrivato!");
					pDataCollectorPipeOut.enqueue(record);
				} while(true);
			}
		};
		tWaitingThread.start();
	}

	public boolean setSamplingTime(long parTime, OnUnsupportedSampleRateOptions parBehaviour) {
		return pFakeDevice.setSamplingFrequency(parTime, parBehaviour);
		
	}
	
	@Override
	public Pipe<Record> registerQuery(ArrayList<String> parAttrbiutesNames) {
		// TODO memorizzare gli attributi da leggere sul dispositivo (sub set degli attributi "totali")
		return pDataCollectorPipeOut;
	}

	@Override
	protected void mngNormalMsg(byte[] parPayload) {
		// TODO Auto-generated method stub	
	}
}